package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.wreport.WReportCusAndVistEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2017/11/27.
 */
@Repository
public class WReportCusAndVisitDao extends BaseJdbcDao {
    public List<WReportCusAndVistEntity> getByCondition(String condition, Object[] params){
        return this.getByPage(condition,params,1,Integer.MAX_VALUE);
    }

    public List<WReportCusAndVistEntity> getByPage(String condition,Object[] params, int requestPage, int pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append("select * from V_WRT_CUSTOMER_VISIT where 1 = 1 ");
        sql.append(condition);

        super.appendPageSql(sql,requestPage,pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(),params);

        List<WReportCusAndVistEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            WReportCusAndVistEntity model = JSON.toJavaObject(jsonList.get(i),WReportCusAndVistEntity.class);
            list.add(model);
        }
        return list;
    }
    public List<WReportCusAndVistEntity> getEntourage(String condition,Object[] params){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "\tt_sys_user.`NAME` AS entourage,\n" +
                "\tt_sys_user.ID AS `entourageId`,\n" +
                "\tt_wrt_customer.`ID` AS `customerId`,\n" +
                "\tt_wrt_customer.`NAME` AS `name`,\n" +
                "\tt_wrt_customer.`TEL` AS `tel`,\n" +
                "\tt_wrt_customer.`SOURCE` AS `source`,\n" +
                "\tt_wrt_customer.`BIRTHDAY` AS `birthDay`,\n" +
                "\tt_wrt_customer.`COMPANY_NAME` AS `companyName`,\n" +
                "\tt_wrt_customer.`CUSTOMER_TYPE` AS `customerType`,\n" +
                "\tt_wrt_customer.`CUSTOMER_LEVEL` AS `customerLevel`,\n" +
                "\tt_wrt_customer.`CUSTOMER_CREDIT` AS `customerCredit`,\n" +
                "\tt_wrt_customer.`BUILD_PROVINCE_ID` AS `buildProvinceId`,\n" +
                "\tt_wrt_customer.`BUILD_CITY_ID` AS `buildCityId`,\n" +
                "\tt_wrt_customer.`BUILD_COUNTY_ID` AS `buildCountyId`,\n" +
                "\tt_wrt_customer.`BUILD_PROVINCE_NAME` AS `buildProvinceName`,\n" +
                "\tt_wrt_customer.`BUILD_CITY_NAME` AS `buildCityName`,\n" +
                "\tt_wrt_customer.`BUILD_COUNTY_NAME` AS `buildCountyName`,\n" +
                "\tt_wrt_customer.`WECHAT` AS `wechat`,\n" +
                "\t`t_wrt_customer_visit`.`ID` AS `id`,\n" +
                "\t`t_wrt_customer_visit`.`VISIT_MODE` AS `visitMode`,\n" +
                "\t`t_wrt_customer_visit`.`INTENTION_MODEL` AS `intentionModel`,\n" +
                "\t`t_wrt_customer_visit`.`BUY_TYPE` AS `buyType`,\n" +
                "\t`t_wrt_customer_visit`.`BUY_DATE` AS `buyDate`,\n" +
                "\t`t_wrt_customer_visit`.`FUND_CONDITION` AS `fundCondition`,\n" +
                "\t`t_wrt_customer_visit`.`BUILD_CONDITION` AS `buildCondition`,\n" +
                "\t`t_wrt_customer_visit`.`OTHER_CONDITION` AS `otherCondition`,\n" +
                "\t`t_wrt_customer_visit`.`COMPETITOR_CONDITION` AS `competitorCondition`,\n" +
                "\t`t_wrt_customer_visit`.`EXISTING_DEVICE` AS `existingDevice`,\n" +
                "\t`t_wrt_customer_visit`.`VISIT_CONTENT` AS `visitContent`,\n" +
                "\t`t_wrt_customer_visit`.`REPORT_DATE` AS `reportDate`,\n" +
                "\t`t_wrt_customer_visit`.`CREATE_USER_ID` AS `createUserId`,\n" +
                "\t`t_wrt_customer_visit`.`ARRIVALS_TIME` AS `arrivalsTime`,\n" +
                "\t`t_wrt_customer_visit`.`LEAVE_TIME` AS `leaveTime`,\n" +
                "\t`t_wrt_customer_visit`.`LEAVE_IMG` AS `leaveImg`,\n" +
                "\t`t_wrt_customer_visit`.`ARRIVALS_LONGITUDE` AS `arrivalsLongitude`,\n" +
                "\t`t_wrt_customer_visit`.`ARRIVALS_LATITUDE` AS `arrivalsLatitude`,\n" +
                "\t`t_wrt_customer_visit`.`ARRIVALS_ADDRESS` AS `arrivalsAddress`,\n" +
                "\t`t_wrt_customer_visit`.`LEAVE_LONGITUDE` AS `leaveLongitude`,\n" +
                "\t`t_wrt_customer_visit`.`LEAVE_LATITUDE` AS `leaveLatitude`,\n" +
                "\t`t_wrt_customer_visit`.`LEAVE_ADDRESS` AS `leaveAddress`\n" +
                "FROM\n" +
                "\t`t_wrt_customer_visit`\n" +
                "LEFT JOIN t_sys_user ON t_wrt_customer_visit.CREATE_USER_ID = t_sys_user.ID\n" +
                "LEFT JOIN t_wrt_customer ON t_wrt_customer.ID = t_wrt_customer_visit.CUSTOMER_ID\n" +
                "WHERE\n" +
                "\t1 = 1");
        sql.append(condition);
        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(),params);
        List<WReportCusAndVistEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            WReportCusAndVistEntity model = JSON.toJavaObject(jsonList.get(i),WReportCusAndVistEntity.class);
            list.add(model);
        }
        return list;

    }
    public WReportCusAndVistEntity getById(String id){
        List<WReportCusAndVistEntity> list = getByCondition(" and id = ?",new String[]{id});
        if ( list != null && list.size() > 0 ){
            return list.get(0);
        } else {
            return null;
        }
    }
}
